package com.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.score.mapper.IFiveoneMapper;
import com.score.pojo.ReturnEntry;
import com.score.pojo.TFiveone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.TreeSet;

@Service
public class FiveoneServiceImpl {
    @Autowired
    private IFiveoneMapper fiveoneMapper;

    /**
     * 学生提交五个一作业
     *
     * @param fiveone
     * @return
     */
    public ReturnEntry sendFiveone(TFiveone fiveone) {
        QueryWrapper<TFiveone> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_num",fiveone.getStuNum())
                .eq("semester",fiveone.getSemester());
        List<TFiveone> tFiveones = fiveoneMapper.selectList(queryWrapper);
        if (tFiveones.size() ==1) {
            int update = fiveoneMapper.update(fiveone, queryWrapper);
            if (update == 1)
                return new ReturnEntry().ok(fiveone, "更新成功");
            return new ReturnEntry().fail(null, "更新失败");
        }else if (tFiveones.size()>1){
            return new ReturnEntry().fail("数据错误",null);
        }
        int insert = fiveoneMapper.insert(fiveone);
        if (insert == 1)
            return new ReturnEntry().ok(fiveone, "提交成功");
        return new ReturnEntry().fail(null, "提交失败");
    }

    /**
     * 老师获取五个一作业
     *
     * @param position
     * @param college
     * @param className
     * @param semester
     * @param workAddress
     * @param currentPage
     * @param pageSize
     * @return
     */
    public ReturnEntry teaFindFiveone(String position,
                                      String college,
                                      String className,
                                      String semester,
                                      String workAddress,
                                      Integer currentPage,
                                      Integer pageSize) {
        QueryWrapper<TFiveone> wrapper = new QueryWrapper<>();
        QueryWrapper<TFiveone> fiveoneQueryWrapper;
        switch (position) {
            case "headmaster":
                fiveoneQueryWrapper = wrapper.eq(!StringUtils.isNullOrEmpty(className), "class_name", className);
                break;
            case "instructor":
            case "deputySecretary":
            case "dean":
                fiveoneQueryWrapper = wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq(!StringUtils.isNullOrEmpty(className), "class_name", className);
                break;
            default:
                return new ReturnEntry().fail("获取失败", null);
        }
        List<TFiveone> tFiveones = fiveoneMapper.selectList(fiveoneQueryWrapper);
        if (StringUtils.isNullOrEmpty(workAddress))
            fiveoneQueryWrapper.eq(!StringUtils.isNullOrEmpty(semester), "semester", semester);
        else if (workAddress.equals("未交"))
            fiveoneQueryWrapper.eq(!StringUtils.isNullOrEmpty(semester), "semester", semester)
                    .eq("work_address", "");
        else
            fiveoneQueryWrapper.eq(!StringUtils.isNullOrEmpty(semester), "semester", semester)
                    .ne("work_address", "");
        Page<TFiveone> fiveonePage = fiveoneMapper.selectPage(new Page<>(currentPage, pageSize), fiveoneQueryWrapper);

        TreeSet<String> classList = new TreeSet<>();
        TreeSet<String> semesterList = new TreeSet<>();
        for (TFiveone fiveone :
                tFiveones) {
            classList.add(fiveone.getClassName());
            semesterList.add(fiveone.getSemester());
        }
        return new ReturnEntry().ok(fiveonePage, classList, semesterList);
    }

}
